<template>
  <div :id="chartId" :style="{height:height,width:width}" class="my-chart" />
</template>
<script>
import echarts from 'echarts'
import { toolboxY } from '@/components/MyCharts/toolbox'
import resize from '@/views/dashboard/admin/components/mixins/resize'
export default {
  mixins: [resize],
  props: {
    chartId: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    dataBar: {
      type: Array,
      default: () => {
        return []
      }
    },
    titleX: {
      type: String,
      default: ''
    },
    titleY: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    dataBar: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById(this.chartId))

      this.setOptions(this.dataBar)
    },
    setOptions(dataBar) {
      const barX = dataBar.map(item => item.name)
      const barY = dataBar.map(item => item.value)
      this.chart.setOption({
        color: ['#3CA0F6'],
        toolbox: toolboxY(this.titleY),
        tooltip: {
          trigger: 'axis',
          axisPointer: { type: 'shadow' }
        },
        grid: { left: '3%', right: '4%', bottom: '3%', top: '5%', containLabel: true },
        xAxis: {
          type: 'value'
        },
        yAxis: {
          type: 'category',
          data: barX,
          axisTick: {
            alignWithLabel: true
          }
        },
        series: [{
          name: this.titleX,
          data: barY,
          type: 'bar',
          barWidth: '30%',
          markLine: {
            data: [
              { type: 'average', name: '平均值' }
            ],
            symbol: 'none',
            itemStyle: {
              normal: {
                lineStyle: {
                  type: 'dashed'
                }
              }
            }
          }
        }]
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.my-chart {
  /deep/ .charts-table {
    width: 100%;
    border: 1px solid #dfe6ec;
    text-align: center;
    border-collapse: collapse;
    >thead{
      >tr{
        >th{
          height: 40px;
          background: #F0F2F5;
          border-right: 1px solid #cecece;
          border-bottom: 1px solid #dfe6ec;
          &:last-child {
            border-right: none;
          }
        }
      }
    }
    >tbody {
      >tr{
        >td{
          height: 40px;
          border-right: 1px solid #cecece;
          border-bottom: 1px solid #dfe6ec;
          &:last-child {
            border-right: none;
          }
        }
        &:last-child {
          >td {
            border-bottom: none;
          }
        }
      }
    }
  }
}
</style>
